package com.cy1640.damai.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.cy1640.damai.bean.Yanchu;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.List;

public class HttpUtils {
	public static boolean isOpenNetWork(Context context) {
		ConnectivityManager manager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (manager.getActiveNetworkInfo() != null) {
			return manager.getActiveNetworkInfo().isAvailable();
		}
		return false;
	}

	public static byte[] getString(String path) {
		try {
			ByteArrayOutputStream baos = new ByteArrayOutputStream();

				URL url = new URL(path);
				HttpURLConnection conn = (HttpURLConnection) url
						.openConnection();
				conn.setRequestMethod("GET");
				conn.setConnectTimeout(3000);
				conn.setDoInput(true);
				int code = conn.getResponseCode();
				if (code == 200) {
					InputStream is = conn.getInputStream();
					int len = 0;
					byte b[] = new byte[1024];
					while ((len = is.read(b)) != -1) {
						baos.write(b, 0, len);
						baos.flush();
					}
					return baos.toByteArray();
				}


		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
	}

	public static Bitmap getImage(String imageUrl, Context context) {
		try {
			if (isOpenNetWork(context)) {
				HttpClient client = new DefaultHttpClient();
				HttpGet get = new HttpGet(imageUrl);
				HttpResponse res = client.execute(get);
				int code = res.getStatusLine().getStatusCode();
				if (code == 200) {
					byte[] b = EntityUtils.toByteArray(res.getEntity());
					Bitmap bm = BitmapFactory.decodeByteArray(b, 0, b.length);
					return bm;
				}
			} else {
				Toast.makeText(context, "无网络!", Toast.LENGTH_SHORT)
						.show();
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public static List<Yanchu> parseJson(String json){
		try {
			JSONObject object =new JSONObject(json);
			JSONArray array =object.getJSONArray("l");
//            List<LBean> list =new ArrayList<>();
//            for (int i = 0; i < array.length(); i++) {
//                JSONObject object1=array.optJSONObject(i);
//                LBean bean =new LBean();
//                bean.setN(object1.optString("n"));
//                bean.setPriceName(object1.optString("priceName"));
//                bean.setT(object1.optString("t"));
//                bean.setVenName(object1.optString("VenName"));
//                bean.setIsXuanZuo(object1.optInt("IsXuanZuo"));
//                bean.setSummary(object1.optString("Summary"));
//                list.add(bean);
//            }

			List<Yanchu> list = JSON.parseArray(array.toString(), Yanchu.class);
			if (list!=null&&list.size()>0){
				return list;
			}else {
				return null;
			}

		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}
}
